//获取应用实例
const app = getApp();

Page({
	data: {
    codename: '获取验证码',
    newcodename: '获取验证码',
    phoneId:'',
    codeId:'',
    oldcodeId:'',
  },
	onLoad: function() {

  },

  accountInput: function (e) {
    var that = this;
    that.setData({
      phoneId: e.detail.value
    })
  },

  codeInput: function (e) {
    var that = this;
    that.setData({
      codeId: e.detail.value
    })
  },

  oldcodeInput: function (e) {
    var that = this;
    that.setData({
      oldcodeId: e.detail.value
    })
  },

  //获取原手机验证码
  getoldphoneCode:function(){
    this.getCode();
  },

  //获取新手机验证码
  getnewphoneCode:function(){
    this.newgetCode();
  },


  //获取新手机验证码
  newgetCode: function () {
    var _this = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Sms&a=sendChangeBanding',
      data: {
        phone: _this.data.phoneId,//
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if(res.data.code==0){
          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              _this.setData({
                newcodename: '重新发送',
                disabled: false
              })

            } else {
              _this.setData({
                newcodename: num + "s"
              })
            }
          }, 1000)
          wx.showToast({
            title: '' + res.data.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'success', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }else{
          wx.showToast({
            title: '' + res.data.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'none', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }
       
        
      }, fail: function () {

      }
    });
  },


  //获取原手机验证码
  getCode: function () {
    var _this = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=User&a=sendChangeBandCode',
      data: {
        token: app.globalData.token,//
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if(res.data.code==0){
          var num = 61;
          var timer = setInterval(function () {
            num--;
            if (num <= 0) {
              clearInterval(timer);
              _this.setData({
                codename: '重新发送',
                disabled: false
              })

            } else {
              _this.setData({
                codename: num + "s"
              })
            }
          }, 1000)
          wx.showToast({
            title: '' + res.data.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'success', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }else{
          wx.showToast({
            title: '' + res.data.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'none', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }
        
      }, fail: function () {

      }
    });
  },


  //验证短信验证码是否正确
  checkCode: function () {
    var _this = this;
    wx.request({
      url: app.globalData.api + '/app.php?c=Sms&a=checkCode',
      data: {
        phone: _this.data.phoneId,//
        code:_this.data.codeId
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          var that = this;
          wx.request({
            url: app.globalData.api + '/app.php?c=User&a=changeBandingPhone',
            data: {
              token: app.globalData.token,
              phone: that.data.phoneId,//
              code: '' + that.data.codeId,
            },
            headers: {
              "Content-Type": "application/x-www-form-urlencoded",
              'platform': '' + app.globalData.platform,
              'version': '' + app.globalData.version,
            },
            method: 'POST',
            dataType: 'json',
            success: function (res) {
              console.log(res);
              wx.showToast({
                title: '' + res.data.msg,//提示文字
                duration: 2000,//显示时长
                mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
                icon: 'success', //图标，支持"success"、"loading"  
                success: function () { },//接口调用成功
                fail: function () { },  //接口调用失败的回调函数  
                complete: function () { } //接口调用结束的回调函数  
              })
            }, fail: function () {

            }
          });
        }else{
          wx.showToast({
            title: '' + res.data.msg,//提示文字
            duration: 2000,//显示时长
            mask: true,//是否显示透明蒙层，防止触摸穿透，默认：false  
            icon: 'none', //图标，支持"success"、"loading"  
            success: function () { },//接口调用成功
            fail: function () { },  //接口调用失败的回调函数  
            complete: function () { } //接口调用结束的回调函数  
          })
        }
        
      }, fail: function () {

      }
    });
  },

  //变更绑定手机号
  changeBandPhone:function(){
    if (this.data.oldcodeId.length==0){
      wx.showToast({
        title: '请输入原手机验证码',
        icon: 'none'
      })
      return;
    }
    if (this.data.phoneId.length==0){
      wx.showToast({
        title: '请输入新手机号码',
        icon: 'none'
      })
      return;
    }
    if (this.data.codeId.length==0){
      wx.showToast({
        title: '请输入新手机验证码',
        icon: 'none'
      })
      return;
    }
    this.checkCode();
  },

});
